#! /bin/sh

# local-mount

pre_mountroot()
{
	run_scripts /scripts/local-top

	echo "debug: root=$root"

	if [ "${root#/dev}" = "$root"  ]; then
		return
	fi

	if [ ! -e "$root" ] || ! $(get_fstype "$root" >/dev/null); then
		log_begin_msg "Waiting for root file system"

		slumber=${rootdelay:-30}
		slumber=$((slumber*10))

		while [ ! -e "$root" ] || ! $(get_fstype "$root" >/dev/null)
		do
			/bin/sleep 0.1
			slumber=$((slumber-1))
			[ $slumber -gt 0 ] || break
		done

		log_end_msg
	fi

	while [ ! -e "$root" ]; do
		case "$root" in
		/dev/hd*)
			suffix="$root#/dev/hd"
			major="${suffix%%[[:digit:]]*}"
			if [ -d "/sys/block/hd$major" ]; then
				echo -n "Warning: boot device may be renamed,"
				echo    "Try root=/dev/hd$suffix"
			fi
			;;
		/dev/sd*)
			suffix="$root#/dev/sd"
			major="${suffix%%[[:digit:]]*}"
			if [ -d "/sys/block/sd$major" ]; then
				echo -n "Warning: boot device may be renamed,"
				echo    "Try root=/dev/sd$suffix"
			fi
		esac
		echo "Gave up waiting for root device. Common problems:"
		echo "  - Boot args (cat /proc/cmdline)"
		echo "  - Check rootdelay= (did the system wait long enough ?)"
		echo "  - Check root= (did the system wait for the right device ?)"
		echo "  - Missing modules (cat /proc/modules; ls /dev)"
		panic "ALERT! $root does not exist. Dropping to a shell !"
	done
}

mountroot()
{
	if [ -z "$root" ]; then
		panic "ALERT! No root specified. Dropping to a  shell !"
	fi

	pre_mountroot

	opt=-w
	[ "$readonly" = "y" ] && opt=-r

	fstype=$(get_fstype "$root")
	# [ "$fstype" != "UNKNOWN" ] && opt="$opt -t $fstype"

	opt="$opt $rootflags"

	[ -d "$rootmnt" ] || mkdir -p "$rootmnt"
	debug_msg "mount $opt $root $rootmnt"
	mount $opt "$root" "$rootmnt"
}
